import {Injectable} from '@angular/core';
import {baseHandleError, BaseService, ReturnForm} from '@sb/base';
import {SysBackupTemplateSearchForm} from '../entity/sys-backup-template-search-form';
import {SysBackupTemplate} from '../entity/sys-backup-template';
import {Observable} from 'rxjs';
import {catchError} from 'rxjs/operators';
import {SFSchema} from '@delon/form';

@Injectable({
    providedIn: 'root'
})
export class SysBackupTemplateService extends BaseService<SysBackupTemplate, SysBackupTemplateSearchForm> {
    readonly basePath = 'sys/backup/template';


    /**
     * 获取
     * @param templateId 模板标识
     */
    getTemplate(templateId: string): Observable<ReturnForm<string>> {
        return this._http.post<ReturnForm<string>>(`${this.url}/getTemplate`, {
            templateId: templateId
        }).pipe(
            catchError(baseHandleError<ReturnForm<string>>('getTemplate'))
        );
    }

    saveTemplate(templateId: string, template: string): Observable<ReturnForm<string>> {
        return this._http.post<ReturnForm<string>>(`${this.url}/saveTemplate`, JSON.stringify({
            templateId: templateId,
            template2: template,
        }), {
            headers: {
                'Content-Type': 'application/json;charset=UTF-8'
            }
        }).pipe(
            catchError(baseHandleError<ReturnForm<string>>('saveTemplate'))
        );
    }

    getSchema(templateId: string): Observable<ReturnForm<SFSchema>> {
        return this._http.post<ReturnForm<SFSchema>>(`${this.url}/getSchema`, {
            templateId: templateId
        }).pipe(
            catchError(baseHandleError<ReturnForm<SFSchema>>('getSchema'))
        );
    }
}
